package com.gemalto.Kryptik.Gui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.Gui.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.*;



public class KeyModePaddingAlgoPanel extends KeyPaddingAlgoPanel {

	private CountedComboBox keyCombo;
	private int historyMax;
	private JRadioButton b1,b2,b3,b4,b5;
	
	/* Load keys from ini-file.
	 * Maximum HISTORY_MAX keys is loaded. If there is no HISTORY_MAX,
	 * we will set it to the default value 5.
	 */
/*	private void getHistory() {
	   String key;
	   int i;

	   historyMax = Kryptik.getIni().getIntValue(algo.toString(),"HISTORY_MAX");
	   if (historyMax>-1){
	   	for (i=1;i<=historyMax;i++){
			key = Kryptik.getIni().getValue(algo.toString(),"KEY_"+i);
			if (key!=null) keyCombo.insertItemAt(key,i-1);
		}
	   } else {
		// There is no HISTORY_MAX, lets set default value 5
	    	Kryptik.getIni().setValue(algo.toString(),"HISTORY_MAX",5);
		historyMax=5;
	   }	
	}
*/


	public KeyModePaddingAlgoPanel(KeyedAlgo algo){
	 	super(algo);

	}

//		this.algo=algo;
/*		newState=new SymmetricAlgoState();

		setLayout(new BoxLayout(this,1));


		JPanel keyPanel=new JPanel();
		keyPanel.setLayout(new BoxLayout(keyPanel,0));
		keyPanel.setBorder(new EmptyBorder(5,5,0,3));
		
		keyCombo = new CountedComboBox(keyLength*2, NewGui.getCounter());
		keyCombo.setEditable(true);
		keyCombo.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
		keyCombo.setMinimumSize(new Dimension(50, 25));  
		keyCombo.setPreferredSize(new Dimension(75, 25)); 
//		keyCombo.setBorder(BorderFactory.createTitledBorder(" Key "));
		keyCombo.setMaximumRowCount(5);
                getHistory();

                JButton keyButton = new JButton("Ok");
		keyButton.setMaximumSize(new Dimension(50, 25));
		keyButton.setMinimumSize(new Dimension(50, 25));  
		keyButton.setPreferredSize(new Dimension(50, 25)); 
              	keyButton.setActionCommand("KEYB-OK");
		keyButton.addActionListener(this);
		
		keyPanel.add(new JLabel(Kryptik.getTranslation("GENALGO_KEY")));
		keyPanel.add(keyCombo);
		keyPanel.add(keyButton);

		add(keyPanel);

		JPanel bottomPanel=new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel,0));

*/

	
		public void initGui(){

				super.initGui();
		
				JPanel modePanel=new JPanel();
				//modePanel.setBorder(new EtchedBorder());
				modePanel.setBorder(BorderFactory.createTitledBorder(Util.getTranslation("GENALGO_MODE")));
				modePanel.setLayout(new BoxLayout(modePanel,1));
				modePanel.setAlignmentY(Component.TOP_ALIGNMENT);
				modePanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); 

			
				b1 = new JRadioButton("ECB");
				b1.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
				b1.setMinimumSize(new Dimension(20, 20)); 
				b1.setPreferredSize(new Dimension(20, 20));
						b1.setActionCommand("MODE-ECB");
				b1.addActionListener(this);
				b1.setSelected(true);

				b2 = new JRadioButton("CBC");
				b2.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
				b2.setMinimumSize(new Dimension(20, 20)); 
				b2.setPreferredSize(new Dimension(20, 20));
						b2.setActionCommand("MODE-CBC");
				b2.addActionListener(this);

				b3 = new JRadioButton("OFB");
				b3.setMaximumSize(new Dimension(Short.MAX_VALUE, 20)); 
				b3.setMinimumSize(new Dimension(20, 20)); 
				b3.setPreferredSize(new Dimension(20, 20));
						b3.setActionCommand("MODE-OFB");
				b3.addActionListener(this);

				ButtonGroup group1 = new ButtonGroup();
				group1.add(b1);
				group1.add(b2);
				group1.add(b3);

				modePanel.add(b1);
				modePanel.add(b2);
				modePanel.add(b3);

				bottomPanel.add(modePanel);

	}

	/* If the key is not in the ComboBox, we will inser it.
	 * History list in INI file is updated.
	 */
/*	private void setKey(String key){
	     	// Add only new key ..
     		boolean newKey = true;
     		for (int i=0;i<keyCombo.getItemCount();i++){
			if (((String)keyCombo.getItemAt(i)).equals(key)){
				newKey = false;
				break;
			}
     		}

     		if ( newKey) {
			keyCombo.insertItemAt(key,0);
     		}


     		// reset key list ...
     		for (int i=0;i<keyCombo.getItemCount();i++){
			if (i>=historyMax) break;
			Kryptik.getIni().setValue(algo.toString(), "KEY_"+(i+1), (String)keyCombo.getItemAt(i));
     		}
	
		// ... and finally set key for this algo
		algo.setKey(new RawSecretKey(algo.getCipherName(),Hex.fromString(key)));

	}

*/

	public void actionPerformed(ActionEvent e){
		super.actionPerformed(e);
		
		if (e.getActionCommand().substring(0,4).equals("MODE")){
			((GenericAlgo)algo).setMode(e.getActionCommand().substring(5));
		} 
	}



	public void update(){
		String mode;
		super.update();
		mode=((GenericAlgo)algo).getMode();
	 	if (mode.equals("ECB")){
			b1.setSelected(true);
		} else if (mode.equals("CBC")) {
			b2.setSelected(true);
		}else if (mode.equals("OFB")) {
			b3.setSelected(true);
		}

	}


}	                         
